#encoding "utf-8"
=begin

Usage:  this script create new stores
Syntax: ruby create-store.rb id, storeName, ownerPhone, ownerName [parallel_type]
        id must > 1
Example ruby create-store.rb 2 测试小店 13600060044 董学锋

=end

require 'securerandom'
load 'rds_api.rb'
load 'crmeb_utils.rb'

id = ARGV[0].to_i
store = ARGV[1]
phone = ARGV[2]
owner = ARGV[3]
pt = ARGV[4] 

if ARGV.size < 4
  puts "Usage:  ruby create-store.rb id storeName ownerPhone ownerName [parallel_type]"
  puts "        id == 0 then create new id"
  exit
end

if id == 0
    sql = 'select max(id) as max_id from crmeb.eb_system_store;'
    res = queryRds sql
    res.each do |r| 
        id = r['max_id'] + 1
        break
    end
end

if mer_id_existed?(id)
  puts "store ##{id} existed"
  exit
end

create_store id, store, owner, phone, pt

delete_store_owners id
create_store_owner id, phone, owner

clear_mer_templates id
copy_mer_templates 0, id

create_group_data id
create_category id, store
